local function onmax(self, max)
	self.inst.replica.musha_staminasleep:SetMax(max)
end

local function oncurrent(self, current)
	self.inst.replica.musha_staminasleep:SetCurrent(current)
end

local function OnTaskTick(inst, self, period)
	self:DoDec(period)
end
--ThePlayer.components.musha_staminasleep:DoDelta(100)
local Musha_Staminasleep =
	Class(
	function(self, inst)
		self.inst = inst
		self.max = 100
		self.current = self.max
		local period = 1
		self.inst:DoPeriodicTask(period, OnTaskTick, nil, self, period)
	end,
	nil,
	{
		max = onmax,
		current = oncurrent
	}
)

function Musha_Staminasleep:OnSave()
	return {
		max = self.max,
		current = self.current or nil
	}
end

function Musha_Staminasleep:OnLoad(data)
	if data ~= nil then
		self.max = data.max
		self.current = data.current
		self:DoDelta(0)
	end
end

function Musha_Staminasleep:LongUpdate(dt)
	self:DoDec(dt, true)
end

function Musha_Staminasleep:GetDebugString()
	return string.format("%2.2f / %2.2f, rate: (%2.2f)", self.current, self.max, self.inst.musha_settings.Dsleep)
end

function Musha_Staminasleep:SetMax(amount)
	self.max = amount
	self.current = amount
end

function Musha_Staminasleep:DoDelta(delta, overtime, ignore_invincible)
	if self.redirect ~= nil then
		self.redirect(self.inst, delta, overtime)
		return
	end

	if not ignore_invincible and self.inst.components.health.invincible or self.inst.is_teleporting then
		return
	end

	local old = self.current
	self.current = math.clamp(self.current + delta, 0, self.max)

	self.inst:PushEvent(
		"musha_staminasleepdelta",
		{oldpercent = old / self.max, newpercent = self.current / self.max, overtime = overtime, delta = self.current - old}
	)
end

function Musha_Staminasleep:GetPercent()
	return self.current / self.max
end

function Musha_Staminasleep:SetPercent(p, overtime)
	local old = self.current
	self.current = p * self.max
	self.inst:PushEvent("musha_staminasleepdelta", {oldpercent = old / self.max, newpercent = p, overtime = overtime})
end

function Musha_Staminasleep:DoDec(dt)
	local regen = 0.1
	if self.inst:HasTag("sleeping") then
		if self.inst.with_fire or self.inst.with_tent then
			regen = self.inst.musha_settings.Dsleep * 1.5
		else
			regen = self.inst.musha_settings.Dsleep
		end
	elseif self.inst.sg:HasStateTag("idle") then
		regen = self.inst.musha_settings.Dsleep * 0.01
	else
		regen = -0.1 * self.inst.musha_settings.Dsleep
	end

	self:DoDelta((dt * regen), true)
end

--[[ function musha_staminasleep:Spelllevels()
	if self.current < 5 then
		self.inst.lowest_sleep = true
	else
		self.inst.lowest_sleep = false
	end

	if self.current >= 5 and self.current < 15 then
		self.inst.vlow_sleep = true
	else
		self.inst.vlow_sleep = false
	end

	if self.current >= 15 and self.current < 42 then
		self.inst.low_sleep = true
	else
		self.inst.low_sleep = false
	end

	if self.current >= 42 and self.current < 73 then
		self.inst.med_sleep = true
	else
		self.inst.med_sleep = false
	end

	if self.current >= 73 then
		self.inst.high_sleep = true
	else
		self.inst.high_sleep = false
	end

	if self.current >= 95 then
		self.inst.full_sleep = true
	else
		self.inst.full_sleep = false
	end
end ]]
return Musha_Staminasleep
